﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BusinnessLogic;
using DataLayer;
using Model;


namespace scrumbench
{
    public partial class AddProductForm : Form
    {
        public AddProductForm(int userid)
        {
            InitializeComponent();
            userIDBack = userid;

        }

        private int userIDBack;

        public int UserIDBack
        {
            get { return userIDBack; }
            set { userIDBack = value; }
        }



        private void AddProductForm_Load(object sender, EventArgs e)
        {
            
            BusinnessLogic.Userproduct BLuserprod = new BusinnessLogic.Userproduct();
            List<Product> userproductlist = BLuserprod.PopulateUserproductlist(userIDBack);

            foreach (Product prod in userproductlist)
            {


                    ListViewItem tempItem = new ListViewItem();
                    tempItem.Text = prod.ToString();
                    tempItem.Tag = prod.productID;

                    ListViewItem.ListViewSubItem nsubitem = new ListViewItem.ListViewSubItem();
                    nsubitem.Text = prod.productID.ToString();
                    tempItem.SubItems.Add(nsubitem);

                    this.ltvProducts.Items.Add(tempItem);




            

                }

            
        }
        private void VulList2(int prodid)
        {
            this.ltvSprints.Items.Clear();

            Userproduct BLsprint = new Userproduct();
            List<Sprint> sprintlist = BLsprint.PopulateSprintlist(prodid);

            foreach (Sprint sprintlol in sprintlist)
            {
              
                    ListViewItem tempItem = new ListViewItem();
                    tempItem.Text = sprintlol.ToString();
                    ListViewItem.ListViewSubItem nsubitem = new ListViewItem.ListViewSubItem();
                    nsubitem.Text = sprintlol.SprintId.ToString();
                    //tempItem.Tag = sprintlol.TeamId;
                    tempItem.SubItems.Add(nsubitem);    

                    ltvSprints.Items.Add(tempItem);

            }

            ltvSprints.Update();
            //listvie2;
        }
        private void VulList3(int sprintid)
        {
            this.ltvUS.Items.Clear();
            
            Userproduct BLuslist = new Userproduct();
            List<UserStory> USlist = BLuslist.PopulateUSList(sprintid);

            foreach (UserStory USlol in USlist)
            {
                ListViewItem tempitem = new ListViewItem();
                tempitem.Text = USlol.Titel;
                ListViewItem.ListViewSubItem nsubitem = new ListViewItem.ListViewSubItem();
                nsubitem.Text = USlol.Userstoryid.ToString();
                tempitem.SubItems.Add(nsubitem);

                //tempitem.Tag = teamid;
                

                ltvUS.Items.Add(tempitem);
            }


        }
        private void VulList4(int USid)
        {
            this.ltvTasks.Items.Clear();
            
            Userproduct BLtaskslist = new Userproduct();
            List<Tasks> taskslist = BLtaskslist.PopulateTaskList(USid);

            foreach (Tasks tasklol in taskslist)
            {
                ListViewItem tempitem = new ListViewItem();
                tempitem.Text = tasklol.Titel.ToString();
                
                ListViewItem.ListViewSubItem nsubitem = new ListViewItem.ListViewSubItem();
                nsubitem.Text = tasklol.TaskID.ToString();
                tempitem.SubItems.Add(nsubitem);

                ltvTasks.Items.Add(tempitem);
            }


        }
        private void listView2_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void ltvProducts_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void ltvProducts_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int tempIndex = (int)ltvProducts.FocusedItem.Tag;

            VulList2(tempIndex);
            this.ltvTasks.Items.Clear();
            this.ltvUS.Items.Clear();

        }

        private void button2_Click(object sender, EventArgs e)//Add User Story
        {
            if (this.ltvSprints.FocusedItem != null)
            {
                NewUserstory tempForm = new NewUserstory(int.Parse(this.ltvSprints.FocusedItem.SubItems[1].Text), this.ltvSprints.FocusedItem.Text);
                tempForm.Show();
            }
            else { MessageBox.Show("Please Select a Sprint before adding a UserStory"); }

        }

        private void button1_Click(object sender, EventArgs e)//Add Product
        {
            NewProduct tempForm = new NewProduct();
            tempForm.Show();
        }

        private void button3_Click(object sender, EventArgs e) //Add Sprint
        {
            if (this.ltvProducts.FocusedItem != null)
            {
                NewSprint tempform = new NewSprint((int)this.ltvProducts.FocusedItem.Tag, ltvProducts.FocusedItem.Text);
                tempform.Show();
            }
            else { MessageBox.Show("Please Select a Product before adding a Sprint"); }
        }

        private void button4_Click(object sender, EventArgs e)//Add Task
        {
            if (this.ltvUS.FocusedItem != null)
            {
                //retrieve team linked to the selected userstory - sprint
                InsertProduct prodinserter = new InsertProduct();
                int teamid = prodinserter.GetTeamidFromUserStory(int.Parse(this.ltvUS.FocusedItem.SubItems[1].Text));

                NewTask tempForm = new NewTask(int.Parse(this.ltvUS.FocusedItem.SubItems[1].Text), teamid, ltvUS.FocusedItem.Text);
                

                tempForm.Show();
            }
            else { MessageBox.Show("Please Select a User Story before adding a Task"); }

        }

        private void ltvSprints_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void ltvSprints_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            
            int sprintid = int.Parse(ltvSprints.FocusedItem.SubItems[1].Text);

            VulList3(sprintid);
            this.ltvTasks.Items.Clear();
        }

        private void ltvUS_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void ltvUS_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            
            int userstoryid = int.Parse(ltvUS.FocusedItem.SubItems[1].Text);

            VulList4(userstoryid);
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void eitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void addProductToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewProduct tempForm = new NewProduct();
            tempForm.Show();
        }

        private void addSprintToSelectedProductToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ltvProducts.FocusedItem != null)
            {
                NewSprint tempform = new NewSprint((int)this.ltvProducts.FocusedItem.Tag, ltvProducts.FocusedItem.Text);
                tempform.Show();
            }
            else { MessageBox.Show("Please Select a Product before adding a Sprint"); }
        }

        private void addUserStoryToSelectedSprintToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ltvSprints.FocusedItem != null)
            {
                NewUserstory tempForm = new NewUserstory(int.Parse(this.ltvSprints.FocusedItem.SubItems[1].Text), this.ltvSprints.FocusedItem.Text);
                tempForm.Show();
            }
            else { MessageBox.Show("Please Select a Sprint before adding a UserStory"); }
        }

        private void addTaskToSelectedUserStorToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            if (this.ltvUS.FocusedItem != null)
            {
                //retrieve team linked to the selected userstory - sprint
                InsertProduct prodinserter = new InsertProduct();
                int teamid = prodinserter.GetTeamidFromUserStory(int.Parse(this.ltvUS.FocusedItem.SubItems[1].Text));

                NewTask tempForm = new NewTask(int.Parse(this.ltvUS.FocusedItem.SubItems[1].Text), teamid, ltvUS.FocusedItem.Text);


                tempForm.Show();
            }
            else { MessageBox.Show("Please Select a User Story before adding a Task"); }
        }
    }
}
